{% extends 'IcsocUIBundle::page.html.twig' %}
{% from 'IcsocUIBundle:Macro:grid.html.twig' import gridWidth,gridNav,gridConfig %}
{% from 'IcsocUIBundle:Macro:actions.html.twig' import create %}
{% block stylesheet %}
{{ parent() }}
    {% stylesheets
    '@datepicker_css'
    '@jqgird_css'
    '@gritter_css'
    '@jquery_ui_css'
    '@chosen_css'
    filter='cssrewrite' filter='uglifycss'
    %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
{% endblock %}

{% block content %}
    <div class="content">
        <p class="alert alert-info" style="width:100%;height:40%;background-color:aliceblue ">
            {% if radioChecked == 3 %}
                <input type="radio" name="evaluate" value="3" class="lbl" checked />
            {% else %}
                <input type="radio" name="evaluate" value="3" class="lbl" />
            {% endif %}
            <label style="margin-bottom:10px;font-size: larger">
                <span for="evaluate1" class="lbl" style="">满意度评价一</span>
            </label><br/>
            <span style="margin-left:20px;margin-top:10px;">
                <span class="lbl">
                    语音选项：按键1=满意 按键2=不满意
                </span>
            </span>
        </p>
        <p class="alert alert-info" style="width:100%;background-color:aliceblue ">
            {% if radioChecked == 2 %}
                <input type="radio" name="evaluate" value="2" class="lbl" checked />
            {% else %}
                <input type="radio" name="evaluate" value="2" class="lbl" />
            {% endif %}
            <label style="margin-bottom:10px;font-size: larger">
                <span for="evaluate1" class="lbl" style="">满意度评价二</span>
            </label><br/>
            <span style="margin-left:20px;margin-top:10px;">
                <span class="lbl">
                    语音选项：按键1=非常满意 按键2=满意 按键3=不满意
                </span>
            </span>
        </p>
        <p class="alert alert-info" style="width:100%;background-color:aliceblue ">
            {% if radioChecked == 4 %}
                <input type="radio" name="evaluate" value="4" class="lbl" checked />
            {% else %}
                <input type="radio" name="evaluate" value="4" class="lbl" />
            {% endif %}
            <label style="margin-bottom:10px;font-size: larger">
                <span for="evaluate1" class="lbl" style="">满意度评价三</span>
            </label><br/>
            <span style="margin-left:20px;margin-top:10px;">
                <span class="lbl">
                    语音选项：按键1=非常满意 按键2=满意 按键3=一般 按键4=不满意 按键5=非常不满意
                </span>
            </span>
        </p>
        <p class="alert alert-info" style="width:100%;background-color:aliceblue ">
            {% if radioChecked == 1 %}
                <input type="radio" name="evaluate" value="1" class="lbl customEvaluate" checked />
            {% else %}
                <input type="radio" name="evaluate" value="1" class="lbl customEvaluate" />
            {% endif %}
            <label style="margin-bottom:10px;font-size: larger">
                <span for="evaluate1" class="lbl" style="">自定义满意度设置</span>
            </label><br/>

            <span class="custom-select alert alert-info" data-condition="0" style="margin-bottom:10px;padding:15px;display:block;width:100%;height:30%;">
                <label style="margin-bottom:20px;">
                    <span class="lbl" style="font-size:14px;">请选择语音</span>
                </label>
                <span style="margin-left:20px;margin-top:10px;">
                    <select class="sound" name="evaluate_sound">
                        <option value="-1">点击选择语音</option>
                                {% for key, sound in sounds %}
                                        <option value="{{ sound.id }}">
                                            {{ sound.name }}
                                        </option>
                                {% endfor %}
                    </select>
                    <audio class="evaluate_player align-middle" style="color: grey;width:29%;margin-left:20px;"  controls="controls" >
                        {{ 'Your browser does not support the player'|trans }}
                    </audio>
                </span><br/>
                <label style="margin-bottom:20px;">
                    <span for="evaluate1" class="lbl" style="font-size:14px;">请选择按键</span>
                </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <select name="evaluateButton" class="chosen-select multiple-select" multiple style="margin-left:20px;width:40%;" >
                    {% for optionId,option in options %}
                            <option value="{{ optionId }}">按键{{ optionId }}</option>
                    {% endfor %}
                </select>
                <span class="chosen-select-names" style="width:40%"></span>
            </span>
            {% if customSelect is defined and customSelect is not empty %}
                {% for k,v in customSelect %}
                    {% if v.evaluate_type is not defined %}
                        <span class="otherEvaluate alert alert-info" style="margin-bottom:10px;padding:15px;display:block;width:100%;height:30%;">
                <label style="margin-bottom:20px;">
                    <span class="lbl" style="font-size:14px;">请选择语音</span>
                </label>
                <span style="margin-left:20px;margin-top:8px;">
                    <select class="evaluate_sound" name="evaluate_sound">
                        <option value="-1">点击选择语音</option>
                        {% if v is defined and v is not empty %}
                            {% for key, sound in sounds %}
                                {% if v.sound_id == sound.id %}
                                    <option value="{{ sound.id }}" selected >
                                        {{ sound.name }}
                                    </option>
                                {% else %}
                                    <option value="{{ sound.id }}">
                                        {{ sound.name }}
                                    </option>
                                {% endif %}
                            {% endfor %}
                        {% else %}
                            {% for key, sound in sounds %}
                                <option value="{{ sound.id }}">
                                    {{ sound.name }}
                                </option>
                            {% endfor %}
                        {% endif %}
                    </select>
                    <audio class="evaluate_player align-middle" style="color: grey;width:29%;margin-left:20px;"  controls="controls" >
                        {{ 'Your browser does not support the player'|trans }}
                    </audio>
                </span><br/>

                <label style="margin-bottom:20px;">
                    <span class="lbl" style="font-size:14px;">请选择按键</span>
                </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            {% set randNum=random(1000) %}
                            <select name="evaluateButton" class="chosen-select-{{ randNum }} multiple-select" multiple style="margin-left:20px;width:40%;" >
                                {% for optionId,option in options %}
                                    {% if v.valid_keys is defined and v.valid_keys is not empty %}
                                        {% if optionId in v.valid_keys %}
                                            <option value="{{ optionId }}" selected >按键{{ optionId }}</option>
                                        {% else %}
                                            <option value="{{ optionId }}">按键{{ optionId }}</option>
                                        {% endif %}
                                    {% else %}
                                        <option value="{{ optionId }}">按键{{ optionId }}</option>
                                    {% endif %}
                                {% endfor %}
                            </select>
                <span class="chosen-select-names-{{ randNum }}" style="width:40%">
                    {% if v.keys is defined %}
                        <br/>
                        {% for kk,kConfig in v.keys %}
                            <span class="evaluateName" style="margin-top:8px;">
                                <label style="margin-left:30px;margin-top:8px;">
                                    <span class="lbl chosenSelectName">按键{{ kk }}
                                        &nbsp;&nbsp;评价语</span>&nbsp;&nbsp;</label>
                                <span class="lbl" style="margin-top:10px;">
                            <input type="text" name="number-{{ kk }}"  value="{{ kConfig.name }}"/></span>
                                {% if kConfig.child is not defined %}
                                    <label class="lbl"  style="margin-left:30px;margin-top:8px;"><input id="secondLevelEvaluate-{{ randNum }}-{{ kk }}" type="checkbox" name="secondEvaluate" style="height: 12px;width: 12px;" />
                                        <span style="font-size:14px;">使用二级满意度</span>
                                    </label>
                                {% endif %}
                                {% if kConfig.child is defined and kConfig.child is not empty %}
                                    <label class="lbl"  style="margin-left:30px;margin-top:8px;"><input id="secondLevelEvaluate-{{ randNum }}-{{ kk }}" checked="checked" type="checkbox" name="secondEvaluate" style="height: 12px;width: 12px;" />
                                        <span style="font-size:14px;">使用二级满意度</span>
                                    </label>
                                    <span class="secondEvaluate alert alert-info" style="margin-bottom:5px;padding:15px;display:block;width:100%;height:30%;">
                                        <label style="margin-bottom:20px;margin-left:160px;">
                                            <span class="lbl" style="font-size:14px;">请选择语音</span>
                                        </label>
                                        <span style="margin-left:20px;margin-top:10px;">
                                            <select class="evaluate_sound" name="evaluate_sound">
                                                <option value="-1">点击选择语音</option>
                                                {% for key, sound in sounds %}
                                                    {% if kConfig.child.sound_id == sound.id %}
                                                        <option value="{{ sound.id }}" selected>
                                                            {{ sound.name }}
                                                        </option>
                                                    {% else %}
                                                        <option value="{{ sound.id }}">
                                                            {{ sound.name }}
                                                        </option>
                                                    {% endif %}
                                                {% endfor %}
                                            </select>
                                            <audio class="evaluate_player align-middle" style="color: grey;width:29%;margin-left:20px;"  controls="controls" >
                                                {{ 'Your browser does not support the player'|trans }}
                                            </audio>
                                        </span><br/>

                                        <label style="margin-bottom:20px;margin-left:160px;">
                                            <span for="evaluate1" class="lbl" style="font-size:14px;">请选择按键</span>
                                        </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                        {% set randNum1 = random(1000) %}
                                        <select name="evaluateButton" class="chosen-select-{{ randNum1 }}-2 multiple-select" multiple style="margin-left:20px;width:40%;" >
                                            {% for optionId1,option1 in options %}
                                                {% if kConfig.child.valid_keys is defined and kConfig.child.valid_keys is not empty %}
                                                    {% if optionId1 in kConfig.child.valid_keys %}
                                                        <option value="{{ optionId1 }}" selected >按键{{ optionId1 }}</option>
                                                    {% else %}
                                                        <option value="{{ optionId1 }}">按键{{ optionId1 }}</option>
                                                    {% endif %}
                                                {% else %}
                                                    <option value="{{ optionId1 }}">按键{{ optionId1 }}</option>
                                                {% endif %}
                                            {% endfor %}
                                        </select>
                                        <br/><span class="chosen-select-names-{{ randNum1 }}-2" style="width:40%">
                                            {% if kConfig.child.keys is defined %}
                                                {% for childKey,childConfig in kConfig.child.keys %}
                                                    <span class="evaluateName">
                                                        <label style="margin-left:200px;margin-top:8px;">
                                                    <span class="lbl chosenSelectName">按键{{ childKey }}
                                                        &nbsp;&nbsp;评价语</span>&nbsp;&nbsp;</label>
                                                    <span class="lbl" style="margin-top:10px;">
                                                    <input type="text" name="number-{{ childKey }}"  value="{{ childConfig.name }}"/></span>
                                                    </span>
                                                    <br/>
                                                {% endfor %}
                                            {% endif %}
                                        </span>
                                    </span>
                                {% endif %}
                            </span><br/>
                        {% endfor %}
                    {% endif %}
                </span>
                <br/>
                <input class="btn delteEvaluate" id="deleteEvaluate-{{ randNum }}" style="margin-top:10px;margin-bottom:10px;border:1px;background-color:dodgerblue;" type="button" value="删除满意度设置" />
            </span>
                    {% endif %}
                {% endfor %}
            {% endif %}

            <label class="lbl oneMoreEvaluate"  style="margin-top:10px;margin-bottom:10px;">
                <input class="addEvaluate" type="checkbox"  style="height: 12px;width: 12px;"/>
                <span style="font-size:14px;">再添加一个满意度问题</span>
            </label>

        </p>

        <button class="btn btn-info" id="saveConfig" type="button">
            <i class="ace-icon fa fa-check bigger-110"></i>
            {{ 'Save'|trans }}
        </button>
    </div>
{% endblock %}
{% block footer %}
    {% javascripts
    '@datepicker_js'
    '@jqgird_js'
    '@jquery_ui_js'
    '@gritter_js'
    '@common_js'
    '@chosen_js'
    filter='uglifyjs'
    %}
    <script src="{{ asset_url }}"></script>
    {% endjavascripts %}
    <script type="text/javascript">
        var number = 0;
        var index = 1;
        $(document).ready(function() {
            $('.custom-select').hide();
            $('.otherEvaluate').hide();
            $('.oneMoreEvaluate').hide();

            $('.evaluate_sound').each(function() {
                if ($(this).val() != -1) {
                    var sound_id = $(this).val();
                    var url = "{{ path('icsoc_sound_play') }}?sound_id="+sound_id;
                    $(this).next().attr("src", url);
                }
            });

            $('.sound').each(function() {
                if ($(this).val() != -1) {
                    var sound_id = $(this).val();
                    var url = "{{ path('icsoc_sound_play') }}?sound_id="+sound_id;
                    $(this).next().attr("src", url);
                }
            });


            var evaluate = $('input:radio[name="evaluate"]:checked').val();
            if (evaluate == 1) {
                $('.otherEvaluate').show();
                $('.oneMoreEvaluate').show();

                for (var i=1;i<=1000;i++) {
                    $('.chosen-select-'+i).chosen();
                    $('.chosen-select-'+i+'-2').chosen();
                    addChosenSelectNames('chosen-select-'+i,'chosen-select-names-'+i,i,1);
                    addSecondLevelEvaluate('chosen-select-'+i,i);
                    addChosenSelectNames('chosen-select-'+i+'-2','chosen-select-names-'+i+'-2',i,2);
                    $('#deleteEvaluate-'+i).click(function() {
                        $(this).parent().remove();
                    });
                }
            }

            $('.evaluate_sound').each(function() {
                $(this).change(function() {
                    if ($(this).val() != -1) {
                        var sound_id = $(this).val();
                        var url = "{{ path('icsoc_sound_play') }}?sound_id="+sound_id;
                        $(this).next().attr("src", url);
                    }
                });
            });

            $('.sound').each(function() {
                $(this).change(function() {
                    if ($(this).val() != -1) {
                        var sound_id = $(this).val();
                        var url = "{{ path('icsoc_sound_play') }}?sound_id="+sound_id;
                        $(this).next().attr("src", url);
                    }
                });
            });

            $('input:radio[name="evaluate"]').click(function() {
                var evaluate = $('input:radio[name="evaluate"]:checked').val();
                if (evaluate == 1) {
                    if ($('.custom-select').next().attr('class') == 'otherEvaluate alert alert-info') {
                        $('.otherEvaluate').show();
                        $('.custom-select').hide();
                        $('.custom-select').attr('data-condition', 0);
                    } else {
                        $('.custom-select').show();
                        $('.chosen-select').chosen();
                        $('.custom-select').attr('data-condition', 1);
                    }
                    $('.oneMoreEvaluate').show();
                    addChosenSelectNames('chosen-select','chosen-select-names',0,1);
                } else {
                    $('.custom-select').hide();
                    $('.custom-select').attr('data-condition', 0);
                    $('.otherEvaluate').hide();
                    $('.oneMoreEvaluate').hide();
                }
            });

            $('.addEvaluate').click(function() {
                if ($(this).is(':checked')) {
                    number++;
                    var newEvaluate = $('.custom-select').clone();
                    newEvaluate.find('.chosen-select').remove();
                    newEvaluate.find('.chosen-container').remove();
                    newEvaluate.find('.chosen-select-names').html("");
                    newEvaluate.find('.sound').attr('class', 'evaluate_sound');
                    newEvaluate.find('.chosen-select-names').attr('class','first-chosen-select-names-'+number);
                    var str = '<select name="evaluateButton" class="chosen-select-'+number+' multiple-select" multiple style="margin-bottom:10px;margin-left:20px;width:40%;" >'+
                            '<option value="1">按键1</option>'+
                            '<option value="2">按键2</option>'+
                            '<option value="3">按键3</option>'+
                            '<option value="4">按键4</option>'+
                            '<option value="5">按键5</option>'+
                            '<option value="6">按键6</option>'+
                            '<option value="7">按键7</option>'+
                            '<option value="8">按键8</option>'+
                            '<option value="9">按键9</option>'+
                            '</select>';
                    var deleteHtml = '<br/><input class="btn delteEvaluate" id="deleteEvaluate-'+number+'" style="margin-top:10px;margin-bottom:10px;border:1px;background-color:dodgerblue;" type="button" value="删除满意度设置" />';
                    newEvaluate.find('.first-chosen-select-names-'+number).before(str);
                    newEvaluate.find('.first-chosen-select-names-'+number).after(deleteHtml);
                    $('.oneMoreEvaluate').before('<span class="alert alert-info otherEvaluate" style="margin-bottom:5px;padding:15px;display:block;width:100%;height:30%;">'+newEvaluate.html()+'</span>');
                    if (number >= 1) {
                        for (var i=1;i<= number;i++) {
                            $('.chosen-select-'+i).chosen();
                            addChosenSelectNames('chosen-select-'+i,'first-chosen-select-names-'+i,i,1);
                            $('#deleteEvaluate-'+i).click(function() {
                                $(this).parent().remove();
                            });
                        }
                    }
                }
             });


            //保存满意度设置
            $('#saveConfig').click(function () {
                var isSubmit = 1;
                $('.evaluate_sound').each(function() {
                    if ($(this).val() == '-1') {
                        $.gritter.add({
                            title: '{{ 'Prompt'|trans }}',
                            text: '请选择语音',
                            class_name: 'gritter-error'
                        });
                        isSubmit = 2;
                        return false;
                    }
                });

                if ($('.custom-select').attr('data-condition') == 1) {
                    $('.sound').each(function() {
                        if ($(this).val() == '-1') {
                            $.gritter.add({
                                title: '{{ 'Prompt'|trans }}',
                                text: '请选择语音',
                                class_name: 'gritter-error'
                            });
                            isSubmit = 2;
                            return false;
                        }
                    });

                    $('.custom-select').find('select').each(function() {
                        if ($(this).val() == null) {
                            $.gritter.add({
                                title: '{{ 'Prompt'|trans }}',
                                text: '请选择按键',
                                class_name: 'gritter-error'
                            });
                            isSubmit = 2;
                            return false;
                        }
                    });
                }

                $('.otherEvaluate').find('select').each(function() {
                    if ($(this).val() == null) {
                        $.gritter.add({
                            title: '{{ 'Prompt'|trans }}',
                            text: '请选择按键',
                            class_name: 'gritter-error'
                        });
                        isSubmit = 2;
                        return false;
                    }
                });


                $('.secondEvaluate').find('select').each(function() {
                    if ($(this).val() == null) {
                        $.gritter.add({
                            title: '{{ 'Prompt'|trans }}',
                            text: '请选择按键',
                            class_name: 'gritter-error'
                        });
                        isSubmit = 2;
                        return false;
                    }
                });

                var totalResult = new Object();
                var evaluate = $('input:radio[name="evaluate"]:checked').val();
                if (evaluate == 1) {
                    var EvaluateConfig = new Object();
                    var soundId = $('.custom-select').children().find('.sound').val();
                    EvaluateConfig["sound_id"] = soundId;
                    var chosenKeys = $.trim($('.custom-select').find('.chosen-select').val());
                    if (chosenKeys != '') {
                        var ValidKeys = chosenKeys;
                        var validkeyArr = ValidKeys.split(',');
                        EvaluateConfig["valid_keys"] = validkeyArr;
                        EvaluateConfig["keys"] = new Object();
                        for (var j=0;j<validkeyArr.length;j++) {
                            if (validkeyArr[j] != '') {
                                EvaluateConfig["keys"][validkeyArr[j]] = new Object();
                                var trueKeyName = $.trim($('#secondLevelEvaluate-0-'+validkeyArr[j]).parent().prev().find('input[name="number-'+validkeyArr[j]+'"]').val());
                                EvaluateConfig["keys"][validkeyArr[j]]["name"] = trueKeyName;
                                if ($('#secondLevelEvaluate-0-'+validkeyArr[j]).is(':checked')) {
                                    var childObj = $('#secondLevelEvaluate-0-'+validkeyArr[j]).parent().next();
                                    var childSoundId = childObj.children(":first").next().find('.evaluate_sound').val();
                                    EvaluateConfig["keys"][validkeyArr[j]]['child'] = new Object();
                                    EvaluateConfig["keys"][validkeyArr[j]]['child']['sound_id'] = childSoundId;
                                    var childChosenKeys = $.trim(childObj.find('.multiple-select').val());
                                    if (childChosenKeys != '') {
                                        var childValidKeys = childChosenKeys;
                                        var childValidKeysArr = childValidKeys.split(',');
                                        EvaluateConfig["keys"][validkeyArr[j]]['child']['valid_keys'] = childValidKeysArr;
                                        EvaluateConfig["keys"][validkeyArr[j]]['child']["keys"] = new Object();
                                        for (var a=0;a<childValidKeysArr.length;a++) {
                                            EvaluateConfig["keys"][validkeyArr[j]]['child']["keys"][childValidKeysArr[a]] = new Object();
                                            if (childValidKeysArr[a] != '') {
                                                var childKeyName = $.trim(childObj.find('.multiple-select').next().next().next().find('span input[name="number-'+childValidKeysArr[a]+'"]').val());
                                                EvaluateConfig["keys"][validkeyArr[j]]['child']["keys"][childValidKeysArr[a]]['name'] = childKeyName;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        totalResult[0] = EvaluateConfig;
                    }

                    $('.otherEvaluate').each(function() {
                        var otherEvaluateConfig = new Object();
                        var otherEvaluateSoundId = $(this).children(":first").next().find('.evaluate_sound').val();
                        otherEvaluateConfig['sound_id'] = otherEvaluateSoundId;
                        var otherchosenKeys = $.trim($(this).find('.multiple-select').val());
                        if (otherchosenKeys != '') {
                            var otherValidKeys = otherchosenKeys;
                            var otherValidKeysArr = otherValidKeys.split(',');
                            otherEvaluateConfig['valid_keys'] = otherValidKeysArr;
                            otherEvaluateConfig['keys'] = new Object();
                            for (var b=0;b<otherValidKeysArr.length;b++) {
                                if (otherValidKeysArr[b] != '') {
                                    otherEvaluateConfig['keys'][otherValidKeysArr[b]] = new Object();
                                    var checkbox = $(this).find('.multiple-select').next().next().find('.evaluateName').children(":first").next().next().find('input[type="checkbox"]').attr('id');
                                    var checkboxId = checkbox.substring(0,checkbox.length-1);
                                    var otherKeyName = $.trim($('#'+checkboxId+otherValidKeysArr[b]).parent().prev().find('input[name="number-'+otherValidKeysArr[b]+'"]').val());
                                    otherEvaluateConfig['keys'][otherValidKeysArr[b]]['name'] = otherKeyName;
                                    if ($('#'+checkboxId+otherValidKeysArr[b]).is(':checked')) {
                                        otherEvaluateConfig["keys"][otherValidKeysArr[b]]['child'] = new Object();
                                        var otherChildObj = $('#'+checkboxId+otherValidKeysArr[b]).parent().next();
                                        var otherchildSoundId = otherChildObj.children(":first").next().find('.evaluate_sound').val();
                                        otherEvaluateConfig["keys"][otherValidKeysArr[b]]['child']['sound_id'] = otherchildSoundId;
                                        var otherchildChosenKeys = $.trim(otherChildObj.find('.multiple-select').val());
                                        if (otherchildChosenKeys != '') {
                                            var otherchildValidKeys = otherchildChosenKeys;
                                            var otherchildValidKeysArr = otherchildValidKeys.split(',');
                                            otherEvaluateConfig["keys"][otherValidKeysArr[b]]['child']['valid_keys'] = otherchildValidKeysArr;
                                            otherEvaluateConfig["keys"][otherValidKeysArr[b]]['child']["keys"] = new Object();
                                            for (var c=0;c<otherchildValidKeysArr.length;c++) {
                                                otherEvaluateConfig["keys"][otherValidKeysArr[b]]['child']["keys"][otherchildValidKeysArr[c]] = new Object();
                                                if (otherchildValidKeysArr[c] != '') {
                                                    var otherchildKeyName = $.trim(otherChildObj.find('.multiple-select').next().next().next().find('span input[name="number-'+otherchildValidKeysArr[c]+'"]').val());
                                                    otherEvaluateConfig["keys"][otherValidKeysArr[b]]['child']["keys"][otherchildValidKeysArr[c]]['name'] = otherchildKeyName;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        totalResult[index] = otherEvaluateConfig;
                        index++;
                    });
                } else if (evaluate == 3) {
                    //满意度评价一
                    var keyName1 = '满意';
                    var keyName2 = '不满意';
                    totalResult[0] = {'evaluate_type':1,'sound_id':1,'valid_keys':[1,2],'keys':{1:{'name':keyName1},2:{'name':keyName2}}};
                } else if (evaluate == 2) {
                    //满意度评价二
                    var keyName1 = '非常满意';
                    var keyName2 = '满意';
                    var keyName3 = '不满意';
                    totalResult[0] = {
                        'evaluate_type':2,'sound_id':2,'valid_keys':[1,2,3],'keys':{1:{'name':keyName1},2:{'name':keyName2},3:{'name':keyName3}}
                    };
                } else if (evaluate == 4) {
                    //满意度评价三
                    var keyName1 = '非常满意';
                    var keyName2 = '满意';
                    var keyName3 = '一般';
                    var keyName4 = '不满意';
                    var keyName5 = '非常不满意';
                    totalResult[0] = {
                        'evaluate_type':3,'sound_id':3,'valid_keys':[1,2,3,4,5],'keys':{1:{'name':keyName1},2:{'name':keyName2},3:{'name':keyName3},4:{'name':keyName4},5:{'name':keyName5}}
                    };
                }
                if (JSON.stringify(totalResult) != '{}') {
                    if (isSubmit == 1) {
                        $.ajax({
                            url: "{{ path('icsoc_evaluate_edit') }}",
                            type: 'post',
                            data: {evaluateConfig: JSON.stringify(totalResult)},
                            success: function (response) {
                                if (response.code != 200) {
                                    $.gritter.add({
                                        title: '{{ 'Prompt'|trans }}',
                                        text: response.message,
                                        class_name: 'gritter-error'
                                    });
                                } else {
                                    $.gritter.add({
                                        title: '{{ 'Prompt'|trans }}',
                                        text: '{{ 'Add success'|trans }}',
                                        class_name: 'gritter-success'
                                    });
                                }
                            }
                        });
                    }
                }
            });

        });

        function processValidKeys(str) {
            var arr = str.split(',');
            var length = arr.length;

            var ids = '';
            for(var i=0;i<length;i++){
                var name = arr[i];
                var chineseRegex = /[^\x00-\xff]/g;
                var newStr = name.replace(chineseRegex,"**");
                var num = newStr.substring(newStr.lastIndexOf('*',newStr)+1);
                ids += num + ',';
            }

            var str = ids.substring(0,ids.length-1);
            return str;
        }

        function addSecondLevelEvaluate(element,numbers)
        {
            var chosenNames = $.trim($('.'+element).val());
            if (chosenNames == '') {
                return false;
            }
            var arr = chosenNames.split(',');
            var length = arr.length;

            for(var i=0;i<length;i++){
                var name = arr[i];
                //var chineseRegex = /[^\x00-\xff]/g;
                //var newStr = name.replace(chineseRegex,"**");
                //var num = newStr.substring(newStr.lastIndexOf('*',newStr)+1);

                $('#secondLevelEvaluate-'+numbers+'-'+name).click(function() {
                    if ($(this).attr('checked') == 'checked') {
                        $(this).removeAttr('checked');
                    }
                    if ($(this).is(':checked')) {
                        var element = $(this).parent().parent();
                        addChosenSelect(element,2,numbers);
                    } else {
                        $(this).parent().next().remove();
                    }
                });
            }
        }

        function addChosenSelectNames(element,container,numbers,level)
        {
            $('.'+element).chosen().change(function() {
                /*if ($('.'+container).find('.evaluateName').find('.secondEvaluate').html() != 'undefined') {
                    var originSecondEvaluate = $('.'+container).find('.evaluateName').find('.secondEvaluate').html();
                    originSecondEvaluate = '<span class="alert alert-info secondEvaluate otherEvaluate" style="margin-bottom:5px;padding-top:15px;padding-left:200px;display:block;width:100%;height:30%;">'+originSecondEvaluate+'</span>';
                }*/
                $('.'+container).html("");
                var chosenNames = $.trim($(this).val());
                if (chosenNames == '') {
                    $('.'+container).html("");
                    return false;
                }
                var arr = chosenNames.split(',');
                var length = arr.length;
                var str = '<br/>';
                for(var i=0;i<length;i++){
                    var name = arr[i];
                    //var chineseRegex = /[^\x00-\xff]/g;
                    //var newStr = name.replace(chineseRegex,"**");
                    //var num = newStr.substring(newStr.lastIndexOf('*',newStr)+1);
                    var num = name;
                    var style = "style='margin-left:40px;margin-top:8px;'";
                    if (level == 2) {
                        style = "style='margin-left:190px;margin-top:8px;'";
                    }
                    str += '<span class="evaluateName" ><label '+style+' ><span class="lbl chosenSelectName">' +'按键'+name+
                            '&nbsp;&nbsp;评价语</span>&nbsp;&nbsp;</label><span class="lbl" style="margin-top:10px;">'+
                            '<input type="text" name="number-'+num+'"  value="'+'按键'+name+'"/></span>';
                    var secondLevel = '<label class="lbl"  style="margin-left:30px;margin-top:8px;">'+'<input id="secondLevelEvaluate-'+numbers+'-'+num+'" type="checkbox" name="secondEvaluate" style="height: 12px;width: 12px;" />'+
                            '<span style="font-size:14px;">使用二级满意度</span>'+
                            '</label>';
                    var str1 = '</span><br/>';

                    if (level != 2) {
                        str = str + secondLevel + str1;
                    } else {
                        str = str + str1;
                    }

                }

                $('.'+container).html(str);
                addSecondLevelEvaluate(element,numbers);
            });
        }

        function addChosenSelect(element,level,numbers)
        {
            number++;
            var newEvaluate = $('.custom-select').clone();
            newEvaluate.find('.chosen-select').remove();
            newEvaluate.find('.chosen-container').remove();
            newEvaluate.find('.chosen-select-names').html("");
            newEvaluate.find('.sound').attr('class', 'evaluate_sound');
            newEvaluate.find('.chosen-select-names').attr('class','chosen-select-names-'+number);

            var str = '<select name="evaluateButton" class="chosen-select-'+number+' multiple-select" multiple style="margin-left:20px;width:40%;" >'+
                    '<option value="1">按键1</option>'+
                    '<option value="2">按键2</option>'+
                    '<option value="3">按键3</option>'+
                    '<option value="4">按键4</option>'+
                    '<option value="5">按键5</option>'+
                    '<option value="6">按键6</option>'+
                    '<option value="7">按键7</option>'+
                    '<option value="8">按键8</option>'+
                    '<option value="9">按键9</option>'+
                    '</select><br/>';
            var deleteHtml = '<input class="btn delteEvaluate" id="deleteEvaluate-'+number+'" style="background-color:#b7b1c6" type="button" value="删除满意度设置" />';
            newEvaluate.find('.chosen-select-names-'+number).before(str);
            if (level != 2) {
                newEvaluate.find('.chosen-select-names-'+number).after(deleteHtml);
            }
            if (level == 2) {
                element.append('<span class="alert alert-info secondEvaluate" style="margin-bottom:5px;padding-top:15px;padding-left:160px;display:block;width:100%;height:30%;">'+newEvaluate.html()+'</span>');
            } else {
                $('.oneMoreEvaluate').before('<span class="alert alert-info otherEvaluate" style="margin-bottom:5px;padding:15px;display:block;width:100%;height:30%;">'+newEvaluate.html()+'</span>');
            }
            if (number >= 1) {
                for (var i=1;i<= number;i++) {
                    $('.chosen-select-'+i).chosen();
                    addChosenSelectNames('chosen-select-'+i,'chosen-select-names-'+i,i,2);

                    $('#deleteEvaluate-'+i).click(function() {
                        $(this).parent().remove();
                    });
                }
            }
        }
    </script>
{% endblock %}